package com.hz.common.net.tls;

import android.net.SSLCertificateSocketFactory;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;



public class TlsSocketFactory extends SSLSocketFactory {
    private String[] a = null;
    private final SSLCertificateSocketFactory b = null;

    private final String c = TlsSocketFactory.class.getSimpleName();

    public TlsSocketFactory(String[] paramArrayOfString) {
        this.a = paramArrayOfString;
    }


    public Socket createSocket() throws IOException {
        return null;
    }


    public Socket createSocket(String paramString, int paramInt) throws IOException {
        return null;
    }


    public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2) throws IOException {
        return null;
    }


    public Socket createSocket(InetAddress paramInetAddress, int paramInt) throws IOException {
        return null;
    }


    public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2, int paramInt2) throws IOException {
        return null;
    }


    public String[] getDefaultCipherSuites() {
        return this.b.getDefaultCipherSuites();
    }


    public String[] getSupportedCipherSuites() {
        return this.b.getSupportedCipherSuites();
    }


    public Socket createSocket(Socket paramSocket, String paramString, int paramInt, boolean paramBoolean) throws IOException {
        if (paramBoolean) {
            paramSocket.close();
        }


        SSLSocket sSLSocket = (SSLSocket) SSLCertificateSocketFactory.getDefault(0).createSocket(InetAddress.getByName(paramString), paramInt);

        String[] arrayOfString1 = {"TLSv1.2", "TLSv1.1"};
        if (this.a != null && this.a.length > 0) {
            arrayOfString1 = this.a;
            (new StringBuilder("SSL protocols' configuration is provided: ")).append(a(arrayOfString1));
        } else {
            (new StringBuilder("SSL default list of protocols will be used: ")).append(a(arrayOfString1));
        }


        String[] arrayOfString2, arrayOfString3;

        if ((arrayOfString3 = a(arrayOfString2 = sSLSocket.getSupportedProtocols(), arrayOfString1)).length == 0) {
            (new StringBuilder("Protocols (")).append(a(arrayOfString1)).append(") are listen in mfac_config.json file are not supported.");

            (new StringBuilder("SSL suppoted protocols will be used: ")).append(a(arrayOfString2));
            sSLSocket.setEnabledProtocols(arrayOfString2);
        } else {
            sSLSocket.setEnabledProtocols(arrayOfString3);
        }


        SSLSession sSLSession = sSLSocket.getSession();

        if (!OkHostnameVerifier.INSTANCE.verify(paramString, sSLSession)) {
            throw new SSLPeerUnverifiedException("Cannot verify hostname: " + paramString);
        }

        (new StringBuilder("Established ")).append(sSLSession.getProtocol()).append(" connection with ").append(sSLSession.getPeerHost()).append(" using ").append(sSLSession.getCipherSuite());
        return sSLSocket;
    }

    private static String[] a(String[] paramArrayOfString1, String[] paramArrayOfString2) {
        ArrayList<String> arrayList = new ArrayList();
        String[] arrayOfString;
        int i;
        byte b;
        for (i = (arrayOfString = paramArrayOfString2).length, b = 0; b < i; ) {
            String str = arrayOfString[b];
            String[] arrayOfString1;
            int j;
            byte b1;
            for (j = (arrayOfString1 = paramArrayOfString1).length, b1 = 0; b1 < j; ) {
                if (arrayOfString1[b1].equalsIgnoreCase(str)) {
                    arrayList.add(str);
                    break;
                }
                b1++;
            }
            b++;
        }
        return arrayList.toArray(new String[arrayList.size()]);
    }

    private static String a(String[] paramArrayOfString) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] arrayOfString;
        int i;
        byte b;
        for (i = (arrayOfString = paramArrayOfString).length, b = 0; b < i; ) {
            String str = arrayOfString[b];
            stringBuilder.append(str).append(";");
            b++;
        }
        return stringBuilder.toString();
    }
}
